package iped.app.timelinegraph;

import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.HashSet;

import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.LegendItemSource;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.DrawingSupplier;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;

public class IpedLegendItemCollection implements LegendItemSource {

    IpedCombinedDomainXYPlot plot;

    static HashMap<String, LegendItem> legends = new HashMap<String, LegendItem>();
    static DrawingSupplier dw = new DefaultDrawingSupplier();

    public IpedLegendItemCollection(IpedCombinedDomainXYPlot plot) {
        this.plot = plot;
    }

    /**
     * Returns the legend items for the plot. Each legend item is generated by the
     * plot's renderer, since the renderer is responsible for the visual
     * representation of the data.
     *
     * @return The legend items.
     */
    @Override
    public LegendItemCollection getLegendItems() {
        HashSet<String> addedLegends = new HashSet<String>();// series legends already included in the chart during the creation of the
                                                             // legends collection

        LegendItemCollection result = new LegendItemCollection();
        XYItemRenderer renderer = plot.getRenderer();

        for (int datasetIndex = 0; datasetIndex < plot.getDatasetCount(); datasetIndex++) {
            XYDataset dataset = plot.getDataset(datasetIndex);
            if (dataset == null) {
                continue;
            }

            if (renderer != null) {
                int seriesCount = dataset.getSeriesCount();
                for (int i = 0; i < seriesCount; i++) {
                    if (renderer.isSeriesVisibleInLegend(i)) {
                        renderer.setPlot((XYPlot) plot.getSubplots().get(datasetIndex));

                        String seriesKey = (String) dataset.getSeriesKey(i);

                        LegendItem item = legends.get(seriesKey);
                        if (item == null) {
                            item = createLegendItem(seriesKey);
                        }

                        item.setSeriesKey(seriesKey);
                        item.setSeriesIndex(i);
                        item.setDataset(dataset);
                        item.setDatasetIndex(datasetIndex);

                        if (!addedLegends.contains(seriesKey)) {
                            addedLegends.add(seriesKey);
                            result.add(item);
                        }
                    }
                }
            }
        }

        return result;
    }

    public Paint getSeriesPaint(String seriesKey) {
        LegendItem li = legends.get(seriesKey);
        if (li == null) {
            li = createLegendItem(seriesKey);
            return li.getFillPaint();
        } else {
            return li.getFillPaint();
        }
    }

    public LegendItem createLegendItem(String seriesKey) {
        Shape shape = new Rectangle2D.Double(-3.0, -3.0, 6.0, 6.0);

        Paint paint = dw.getNextPaint();

        LegendItem item = new IpedLegendItem(seriesKey, paint, plot);
        item.setShape(shape);

        item.setLineVisible(true);
        item.setLine(shape);
        item.setLinePaint(paint);
        item.setFillPaint(paint);
        item.setShapeVisible(true);

        legends.put(seriesKey, item);

        return item;
    }

}
